﻿using System;
using System.Dynamic;
using System.Linq;

namespace _142_模拟委托实例的调用_动态类型
{
    class Program
    {
        static void Main(string[] args)
        {
            dynamic d =new 自定义类();
            int r = d(2, 10, 15, 7);

            Console.WriteLine($"计算结果：{r}");
            Console.Read();

        }
    }

    //从DynamicObject派生一个自定义类，并重写TryInvoke方法
    public class 自定义类 : DynamicObject
    {
        public override bool TryInvoke(InvokeBinder binder, object[] args, out object result)
        {
            result = 0;
            int temp = 0;

            foreach (var item in args.Cast<int>())
            {
                temp += item;
            }
            result = temp;
            return true;
        }

    }
}
